
package com.abcijkxyz.dialects.stsm.score;

import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.springframework.context.ApplicationContext;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.spring4.context.SpringContextUtils;
import org.thymeleaf.templatemode.TemplateMode;
import org.unbescape.html.HtmlEscape;

import com.abcijkxyz.domain.extrathyme.Headline;
import com.abcijkxyz.repository.extrathyme.HeadlineRepository;

public class HeadlinesElementTagProcessor extends AbstractElementTagProcessor {

	private static final String	TAG_NAME	= "headlines";
	private static final int	PRECEDENCE	= 1000;

	private final Random		rand		= new Random(System.currentTimeMillis());

	public HeadlinesElementTagProcessor(final String dialectPrefix) {
		super(TemplateMode.HTML, // This processor will apply only to HTML mode
				dialectPrefix, // Prefix to be applied to name for matching
				TAG_NAME, // Tag name: match specifically this tag
				true, // Apply dialect prefix to tag name
				null, // No attribute name: will match by tag name
				false, // No prefix to be applied to attribute name
				PRECEDENCE); // Precedence (inside dialect's own precedence)
	}

	@Override
	protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler) {

		/*
		 * Obtain the Spring application context.
		 */
		final ApplicationContext appCtx = SpringContextUtils.getApplicationContext(context);

		/*
		 * Obtain the HeadlineRepository bean from the application context, and ask it for the current list of headlines.
		 */
		final HeadlineRepository headlineRepository = appCtx.getBean(HeadlineRepository.class);
		final List<Headline> headlines = headlineRepository.findAllHeadlines();

		/*
		 * Read the 'order' attribute from the tag. This optional attribute in our tag will allow us to determine whether we want to show a random headline or only the latest one ('latest' is default).
		 */
		final String order = tag.getAttributeValue("order");

		String headlineText = null;
		if (order != null && order.trim().toLowerCase().equals("random")) {
			// Order is random

			final int r = this.rand.nextInt(headlines.size());
			headlineText = headlines.get(r).getText();

		} else {
			// Order is "latest", only the latest headline will be shown

			Collections.sort(headlines);
			headlineText = headlines.get(headlines.size() - 1).getText();

		}

		/*
		 * Create the DOM structure that will be substituting our custom tag. The headline will be shown inside a '<div>' tag, and so this must be created first and then a Text node must be added to it.
		 */
		final IModelFactory modelFactory = context.getModelFactory();

		final IModel model = modelFactory.createModel();

		model.add(modelFactory.createOpenElementTag("div", "class", "headlines"));
		model.add(modelFactory.createText(HtmlEscape.escapeHtml5(headlineText)));
		model.add(modelFactory.createCloseElementTag("div"));

		/*
		 * Instruct the engine to replace this entire element with the specified model.
		 */
		structureHandler.replaceWith(model, false);

	}

}
